/**
 * Created by zyy on 15/6/26.
 * zhangyuyu@superjia.com
 * user:{
 *     username:'', pinyin
 *     password: '', 密码
 *     nickname: '', 昵称
 *     photo: '', 头像 /photo/xxx.png
 *     role: '', admin:管理员, 
 *     createTime:'', 创建时间
 * }
 */

// 插入管理员
// var users = [
//         {username:'yuqingyang',password: 'yuqingyang', nickname: '于清扬', photo: 'iwjw', role:'member', createTime: '1440385216200'},
//         {username:'zhanshanqin',password: 'zhanshanqin', nickname: '湛善钦', photo: 'zhanshanqin', role:'member', createTime: '1440385216200'},
//         {username:'yangenpeng',password: 'yangenpeng', nickname: '杨恩朋', photo: 'yangenpeng', role:'member', createTime: '1440385216200'},
//         {username:'zhangyuyu',password: 'zhangyuyu', nickname: '张玉瑜', photo: 'iwjw', role:'member', createTime: '1440385216200'},
//         {username:'baiwenhao',password: 'baiwenhao', nickname: '白文浩', photo: 'iwjw', role:'member', createTime: '1440385216200'},
//         {username:'lancui',password: 'lancui', nickname: '兰翠', photo: 'iwjw', role:'member', createTime: '1440385216200'},
//         {username:'zhuxinyong',password: 'zhuxinyong', nickname: '朱信勇', photo: 'zhuxinyong', role:'member', createTime: '1440385216200'},
//         {username:'liyaping',password: 'liyaping', nickname: '李亚平', photo: 'liyaping', role:'member', createTime: '1440385216200'}
//     ]

// db.user.insert(users)

var router = require('koa-router')({
        prefix: '/user'
    }
);

var wrap = require('co-monk')
var parse = require('co-body')

var filter = require('../common/filter')
var utils = require('../common/utils')
var db = require('../db')

var userDao = wrap(db.get('user'))

// 登录
router.post('/login', function *(next) {

    var user = this.locals._user

    var body = yield parse(this)
    if (user && user.username == body.username) {
        utils.success(this, user)
        return
    }

    var user = yield userDao.findOne({username: body.username});

    if (!user) {
        utils.failed(this, 10004)
        return
    } else {
        this.session.user = utils.extend({}, user)

        utils.success(this,user)
    }
})

// 所有用户
router.get('/userlist', filter.login, function *(next) {
    var user = this.locals._user

    var user = yield userDao.find();

    utils.success(this, utils.wrapUser(user))
})

// 注册
router.post('/signin', filter.login, function *(next) {
    var body = yield parse(this)
    var username = body.username;

    var user = yield userDao.findOne({username: username})

    // 如果查到相同用户;
    if (user) {
        utils.failed(this, 10002);
    } else {
        var newuser = {
            username: body.username,
            nickname: body.nickname,
            password: body.password || 'iwjw2015',
            photo: body.photo || 'wk',
            createTime: Date.now(),
            role: 'admin'
        }

        yield userDao.insert(newuser);
        utils.success(this, newuser)
    }
})

// 删除用户
router.post('/delete', filter.login, function *(next) {
    var body = yield parse(this)

    var member = yield userDao.findOne({_id: body.id})

    if(!member){
        utils.failed(this, 10004)
    }else{
        console.log(member.leader)
        member.leader = _.without(member.leader, this.user._id)
        console.log(member.leader)
        yield userDao.updateById(member._id, member)
        utils.success(this, utils.wrapUser(member))
    }
})


module.exports = router